package cn.jhc.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import cn.jhc.bean.Admin;
import cn.jhc.bean.User;
import cn.jhc.db.AdminDao;
import cn.jhc.db.UserDao;
import cn.jhc.utils.Constants;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/admin/adminlogin.do")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DataSource dataSource = (DataSource)request.getServletContext().getAttribute(Constants.H2_DATASOURCE);
		String username = request.getParameter("username"); 
		String password = request.getParameter("password"); 
		System.out.println(username);
		System.out.println(password);
		AdminDao adminDao = new AdminDao(dataSource);
	    Admin  admin = new Admin();
		admin.setUsername(username);
        admin.setUsername(password);  
        request.getSession().setAttribute("sign",password);
		if (adminDao.selectbyname(admin)==true) {
			response.sendRedirect("add.jsp");
		}else {
			response.sendRedirect("../other/AdminLoginfilure.jsp");
		}
	}
	}


